//
//  HomeVCCell.swift
//  weibo1
//
//  Created by 李进 on 15/10/21.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit
/// 微博 Cell 中控件的间距数值
let HomeCellMargin: CGFloat = 12
/// 微博头像的宽度
let HomeCellIconWidth: CGFloat = 35

class HomeVCCell: UITableViewCell {
    
    /// 微博视图模型
    var viewModle: userFriendViewModel? {
        didSet {
            TopView.viewModle = viewModle
            
            TextLabel.text = viewModle?.status.text
            
            COllCell.viewModel = viewModle
            
            COllCell.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(COllCell.bounds.height)
                make.width.equalTo(COllCell.bounds.width)
            }
        }
    }

    /// 根据指定的视图模型计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns: 返回视图模型对应的行高
    func rowHeight(vm: userFriendViewModel) -> CGFloat {
        // 1. 记录视图模型 -> 会调用上面的 didSet 设置内容以及更新`约束`
        viewModle = vm
        
        // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        
        // 3. 返回底部视图的最大高度
        return CGRectGetMaxY(BootView.frame)
    }
    //懒加载控件
    private lazy var TopView:HomeVCTopCell = HomeVCTopCell()
    
    
    //正文
     lazy var TextLabel:UILabel = UILabel(title: "微博中正文", fontSize: 15, color: UIColor.grayColor(), screenInset: HomeCellMargin)
    
    //配图视图
    lazy var COllCell: CollView = CollView()
    //底部
    lazy var BootView:HomeVCBootoCell = HomeVCBootoCell()
    
    //构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


// MARK: - 设置ui
extension HomeVCCell {
    
    func setupUI(){
        
        contentView.addSubview(TopView)
        contentView.addSubview(TextLabel)
        contentView.addSubview(COllCell)
        contentView.addSubview(BootView)
        
        // 2. 自动布局
        // 1> 顶部视图
        TopView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * HomeCellMargin + HomeCellIconWidth)
        }
        
        // 2> 内容标签
        TextLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(TopView.snp_bottom).offset(HomeCellMargin)
            make.left.equalTo(contentView.snp_left).offset(HomeCellMargin)
           
        }
        
//        //3.>配图视图的自动布局
//        COllCell.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(TextLabel.snp_bottom).offset(HomeCellMargin)
//            make.left.equalTo(TextLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//           
//        }
        
        
        //4.>底部视图按钮
        BootView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(COllCell.snp_bottom).offset(HomeCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
            
           // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
    
}